package com.ph.shopping.facade.order.service.impl;

import cm.ph.shopping.facade.order.entity.PhAgentOrder;
import cm.ph.shopping.facade.order.entity.PhMerchantOrder;
import cm.ph.shopping.facade.order.service.IAgentOrderService;
import cm.ph.shopping.facade.order.vo.PhAgentOrderListVo;
import cm.ph.shopping.facade.order.vo.QueryOrderDetailVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.transform.MoneyTransUtil;
import com.ph.shopping.facade.mapper.PhAgentOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 代理商进货单 实现类
 * @project：phshopping-service-order
 * @description: TODO 
 * @author lichao  
 * @date 2017年3月27日
 */
@Component
@Service(version = "1.0.0")
public class PhAgentOrderServiceImpl extends BaseService implements IAgentOrderService {

	@Autowired
	private PhAgentOrderMapper phAgentOrderMapper;

    private MoneyTransUtil moneyTransUtil;

    /**
	 * 进货单列表
	 */
	@Override
	public Result getAgentOrderList(PageBean pageBean, PhAgentOrderListVo phAgentOrderListVo) throws Exception {
		PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize(), "createTime desc");
		List<PhAgentOrderListVo> list = (Page<PhAgentOrderListVo>)phAgentOrderMapper.getAgentOrderList(phAgentOrderListVo);
		PageInfo<PhAgentOrderListVo> pageInfo = new PageInfo<>(list);
		return new Result(ResultEnum.SUCCESS.getCode(),"" , pageInfo.getList(), pageInfo.getTotal());
	}

    @Override
    public QueryOrderDetailVo getOrderDetail(QueryOrderDetailVo queryOrderDetailVo) {
        QueryOrderDetailVo orderDetailVo = phAgentOrderMapper.getOrderDetail(queryOrderDetailVo);
        orderDetailVo.setOrderProductsList(phAgentOrderMapper.getOrderProductList(queryOrderDetailVo));
        return orderDetailVo;
    }
    @Override
    public Result updateLogistics(PhAgentOrder phAgentOrder) {
        BigDecimal money = moneyTransUtil.transMulti(phAgentOrder.getMoney());
        BigDecimal freight = moneyTransUtil.transMulti(phAgentOrder.getFreight());
        BigDecimal totalCost = money.add(freight);
        phAgentOrder.setMoney(money);
        phAgentOrder.setFreight(freight);
        phAgentOrder.setTotalCost(totalCost);
        phAgentOrderMapper.updateByPrimaryKeySelective(phAgentOrder);
        return getResult(ResultEnum.SUCCESS);
    }

    @Override
    public Result updateLogisticsInfo(PhAgentOrder phAgentOrder) {
        phAgentOrderMapper.updateByPrimaryKeySelective(phAgentOrder);
        return getResult(ResultEnum.SUCCESS);
    }

	@Override
	public Result updateForShipping(PhAgentOrder phAgentOrder) {
		   int  msg=phAgentOrderMapper.updateByPrimaryKeySelective(phAgentOrder);
		   if (msg == 1) {
				return getResult(ResultEnum.SUCCESS);
			} else {
				return getResult(ResultEnum.SHIPPING_DEFAULT_FAIL);
			}
	}
		
	/**
	 * 查询订单单条数据
	 * @param agentOrder
	 * @return
	 *
	 */
	@Override
	public  	PhAgentOrder   findAgentOrderByTarget(PhAgentOrder agentOrder){
				return   this.phAgentOrderMapper.selectOne(agentOrder);
	}
	
	@Override
	public int updateForOrderStatus(PhAgentOrder phAgentOrder) {
		   int  msg=phAgentOrderMapper.updateByPrimaryKeySelective(phAgentOrder);
		   return  msg;
	}
	
}
